function getScript(){
    if ($myjq !== undefined){
        sortbypts = !sortbypts;
        mainSort();
    }else{
        sortbypts = true;
        var script=document.createElement('script');
        script.src='//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js';
        var head=document.getElementsByTagName('head')[0], done=false;
        script.onload=script.onreadystatechange = function(){
            if ( !done && (!this.readyState
                || this.readyState == 'loaded'
                || this.readyState == 'complete') ) {
                done=true;
                $myjq=jQuery.noConflict();
                window.setTimeout(mainSort,50);
                script.onload = script.onreadystatechange = null;
                head.removeChild(script);
            }
        };
        head.appendChild(script);
    }
}
function mainSort(){
    switch(window.location.hostname)
    {
    case 'news.ycombinator.com':      
      sortFeed();
      break;
    // case 'www.quora.com':
      // feedParent = $myjq('.pagedlist_item').parent();
      // sortFeed();
      // break;
    default:      
        alert('Sorting not enabled for '+window.location.hostname)
    }
}
function scrollToBottom(){
    $myjq(document).scrollTop($myjq(document).scrollTop()+$myjq(document).height());
}
function getNumber(txt){
    var num = 0;
    try{
        num = parseInt(txt);
        if (num.toString() != txt) {
            num = parseFloat(txt.replace(/[kK]/g,''))*1000;
        num = isNaN(num) ? 0 : num;
    }
    }catch(e){console.log('Fail in getNumber '+e.message);}
    return num;
}
function sortFeed(){
    $myjq(document).scrollTop(0);
    var feedParent;
    try {
        feedParent = $myjq('table:eq(2)');
        fd=$myjq('table:eq(2) tr').detach();
        fd2d = [];
        while((slice = fd.splice(0,3)).length > 0) {fd2d.push(slice);}      
        //list1 = $myjq('table:eq(2) tr:nth-child(3n+2)'); //sort this
        //list2 = $myjq('table:eq(2) tr:nth-child(3n+1)'); //move along with list1
        //list3 = $myjq('table:eq(2) tr:nth-child(3n+3)'); //most along with list1
        fd2d.sort(function(a,b){
            try{
                sortClue = (sortbypts) ? '[id^=score]' : '[href^=item]';
                var aa = getNumber($myjq(a[1]).find(sortClue).text());
                var bb = getNumber($myjq(b[1]).find(sortClue).text());
                aa = (typeof aa !== 'undefined') ? aa : 0;
                bb = (typeof bb !== 'undefined') ? bb : 0;
                return bb-aa;
            }catch(e){console.log('Fail in sort '+e.message);}
        });
    }catch(e){
        console.log('Fail in sortFeed '+e.message);
    }finally{
        fd = [];
        for(var i=0; i<fd2d.length; i++){fd=fd.concat(fd2d[i])}
        feedParent.prepend(fd);
    }
}
function pageDown(){
    for(var i=0; i<pgdn; i++){
        window.setTimeout(scrollToBottom,pgdnTimer);
        pgdnTimer = (i+1)*timedelay;
    }
    window.setTimeout(sortFeed,pgdnTimer+100);
}
var $myjq;
////Automatic page down attempts
////var pgdn = parseInt(window.prompt("Number of attempts to load more content",10)) || 0;
////var pgdnTimer = 0;
////var timedelay = 2000; //2 seconds
var sortbypts //Sort by points (sortbypts=true) or comments (sortbypts=false)
getScript();